<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Cache,Event,Auth;
class SafeController extends Controller {

    //重要部位查询
    public function zybwcx(request $request)
    {
        if($request->rows) {
            $map = array();
            $query = DB::table('zhongyaobuwei');
            if(Auth::guard('admin')->user()->dengji == 2){
                $map['city'] = Auth::guard('admin')->user()->city;
            }
            if(Auth::guard('admin')->user()->dengji == 3){
                $map['area'] = Auth::guard('admin')->user()->area;
            }
            if(Auth::guard('admin')->user()->dengji == 4){
                $unit_id = db::table('units')->where('minjing',Auth::guard('admin')->user()->id)->pluck('id');
                $map['org_id'] = Auth::guard('admin')->user()->org_id;
                $query = DB::table('zhongyaobuwei')->whereIn('unit_id',$unit_id);
            }

            if($request->city != null){
                if($request->city == 0){

                }else{
                    $map['city'] = $request->city;
                }
            }
            if($request->area){
                $map['area'] = $request->area;
            }
            if($request->org_id){
                $map['org_id'] = $request->org_id;
            }
            if($request->unit_name){
                $unit_id = db::table('units')->where('name',$request->unit_name)->value('id');
                $map[] = array('unit_id',$unit_id);
            }
            if($request->name){
                $map[] = array('name','like','%'.$request->name.'%');
            }
            if($request->city == null && $request->area == null && $request->org_id == null && $request->unit_name == null && $request->name == null){
                die;
            }
            $res = $query->where($map)->paginate($request->rows);
            $list = $res->items();
            foreach ($list as $k=>$v){
                $list[$k]->city_name = getValue($v->city,'area' ,'id' ,'name' );
                $list[$k]->area_name = getValue($v->area,'area' ,'id' ,'name' );
                $list[$k]->org_name = getValue($v->org_id,'area' ,'id' ,'name' );
                $list[$k]->unit_name = getValue($v->unit_id,'units' ,'id' ,'name' );
            }
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.safe.zybwcx');
        }
    }

    public function zybw_list($id)
    {
        $res = DB::table('zhongyaobuwei')->where('unit_id',$id)->paginate(10);
        $count = DB::table('zhongyaobuwei')->where('unit_id',$id)->count();
        return view('admin.safe.zybw_list', ['res' => $res,'count'=>$count,'id'=>$id]);
    }

    public function ckzybw($id)
    {
        $re = DB::table('zhongyaobuwei')->where('id',$id)->first();
        return view('admin.safe.ckzybw', ['re' => $re,'id'=>$id]);
    }

    public function gzry_list($id)
    {
        $res = DB::table('gongzuorenyuan')->where('buwei_id',$id)->paginate(10);
        $count = DB::table('gongzuorenyuan')->where('buwei_id',$id)->count();
        $unit_id = DB::table('zhongyaobuwei')->where('id',$id)->value('unit_id');
        return view('admin.safe.gzry_list', ['res' => $res,'count'=>$count,'id'=>$id,'unit_id'=>$unit_id]);
    }

    public function ckgzry($id)
    {
        $re = DB::table('gongzuorenyuan')->where('id',$id)->first();
        return view('admin.safe.ckgzry', ['re' => $re]);
    }


    //安防设施查询
    public function afsscx(request $request)
    {
        if(Auth::guard('admin')->user()->org_id != null){
            $map['org_id'] = Auth::guard('admin')->user()->org_id;
        }
        if($request->isMethod('post')){
            if(input::get('name')){
                $map[] = array('name','like','%'.input::get('name').'%');
            }
            if(input::get('org_id')){
                $map['area_id'] = input::get('org_id');
            }
        }

        $map['is_del'] = '0';
        $res = DB::table('units')->where($map)->orderby('id','desc')->paginate(10);
        $count = DB::table('units')->where($map)->count();

        return view('admin.safe.afsscx', ['res' => $res,'count'=>$count,'types'=>'zybwcx']);
    }

    public function ckafss($id)
    {
        $type = db::table('units')->where('id',$id)->value('type');
        if(!$type){
            echo "<script>alert('确认单位类型后才能编辑安防设施情况');window.history.go(-1);</script>";
            exit();
        }
        $re = DB::table('anfangsheshi')->where('unit_id',$id)->first();
        return view('admin.safe.ckafss', ['re' => $re,'type'=>$type]);
    }


    //保卫机构查询
    public function bwjgcx(request $request)
    {
        if(Auth::guard('admin')->user()->org_id != null){
            $map['org_id'] = Auth::guard('admin')->user()->org_id;
        }
        if($request->isMethod('post')){
            if(input::get('name')){
                $map[] = array('name','like','%'.input::get('name').'%');
            }
            if(input::get('org_id')){
                $map['area_id'] = input::get('org_id');
            }
        }

        $map['is_del'] = '0';
        $res = DB::table('units')->where($map)->orderby('id','desc')->paginate(10);
        $count = DB::table('units')->where($map)->count();

        return view('admin.safe.bwjgcx', ['res' => $res,'count'=>$count,'types'=>'zybwcx']);
    }

    public function bwjg_list($id)
    {
        $res = DB::table('jigou')->where('unit_id',$id)->paginate(10);
        $count = DB::table('jigou')->where('unit_id',$id)->count();
        return view('admin.safe.bwjg_list', ['res' => $res,'count'=>$count,'id'=>$id]);
    }

    public function ckbwjg($id)
    {
        $re = DB::table('jigou')->where('id',$id)->first();
        return view('admin.safe.ckbwjg', ['re' => $re]);
    }


    //保卫力量查询
    public function bwllcx(request $request)
    {
        if($request->rows) {
            $map = array();
            $query = DB::table('baoweirenyuan');
            if(Auth::guard('admin')->user()->dengji == 2){
                $map['city'] = Auth::guard('admin')->user()->city;
            }
            if(Auth::guard('admin')->user()->dengji == 3){
                $map['area'] = Auth::guard('admin')->user()->area;
            }
            if(Auth::guard('admin')->user()->dengji == 4){
                $unit_id = db::table('units')->where('minjing',Auth::guard('admin')->user()->id)->pluck('id');
                $map['org_id'] = Auth::guard('admin')->user()->org_id;
                $query = DB::table('baoweirenyuan')->whereIn('unit_id',$unit_id);
            }
            if($request->city != null){
                if($request->city == 0){

                }else{
                    $map['city'] = $request->city;
                }
            }
            if($request->area){
                $map['area'] = $request->area;
            }
            if($request->org_id){
                $map['org_id'] = $request->org_id;
            }
            if($request->unit_name){
                $unit_id = db::table('units')->where('name',$request->unit_name)->value('id');
                $map[] = array('unit_id',$unit_id);
            }
            if($request->name){
                $map[] = array('name','like','%'.$request->name.'%');
                //$map['name'] = $request->name;
            }
            if($request->id_number){
                $map[] = array('id_number','like','%'.$request->id_number.'%');
            }
            if($request->city == null && $request->area == null && $request->org_id == null && $request->unit_name == null && $request->name == null && $request->id_number == null){
                die;
            }
            //getLastSql();
            $res = $query->where($map)->paginate($request->rows);
            $list = $res->items();
            foreach ($list as $k=>$v){
                if($v->guanxi == '外聘'){
                    if($v->baoan_id){
                        $v->kaohe = '1';
                        $is_hege = db::table('baoan_kaohe')->where('baoan_id',1)->orderby('id','desc')->value('is_hege');
                        if($is_hege == 1){
                            $v->kaohe = '合格';
                        }elseif ($is_hege == 2){
                            $v->kaohe = '不合格';
                        }
                    }else{
                        $v->kaohe = '暂无考核';
                    }
                }else{
                    $v->kaohe = '暂无考核';
                }
                $list[$k]->city_name = getValue($v->city,'area' ,'id' ,'name' );
                $list[$k]->area_name = getValue($v->area,'area' ,'id' ,'name' );
                $list[$k]->org_name = getValue($v->org_id,'area' ,'id' ,'name' );
                $list[$k]->unit_name = getValue($v->unit_id,'units' ,'id' ,'name' );
            }
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.safe.bwllcx');
        }
    }

    public function bwll_list($id)
    {
        $res = DB::table('baoweirenyuan')->where('unit_id',$id)->paginate(10);
        $count = DB::table('baoweirenyuan')->where('unit_id',$id)->count();
        return view('admin.safe.bwll_list', ['res' => $res,'count'=>$count,'id'=>$id]);
    }

    public function ckbwll($id)
    {
        $re = DB::table('baoweirenyuan')->where('id',$id)->first();
        $nation = db::table('nation')->get();
        return view('admin.safe.ckbwll', ['re' => $re,'id'=>$id,'nation'=>$nation]);
    }


    //保卫制度查询
    public function bwzdcx(request $request)
    {
        if(Auth::guard('admin')->user()->org_id != null){
            $map['org_id'] = Auth::guard('admin')->user()->org_id;
        }
        if($request->isMethod('post')){
            if(input::get('name')){
                $map[] = array('name','like','%'.input::get('name').'%');
            }
            if(input::get('org_id')){
                $map['area_id'] = input::get('org_id');
            }
        }

        $map['is_del'] = '0';
        $res = DB::table('units')->where($map)->orderby('id','desc')->paginate(10);
        $count = DB::table('units')->where($map)->count();

        return view('admin.safe.bwzdcx', ['res' => $res,'count'=>$count,'types'=>'zybwcx']);
    }

    public function bwzd_list($id)
    {
        $res = DB::table('zhidu')->where('unit_id',$id)->paginate(10);
        $count = DB::table('zhidu')->where('unit_id',$id)->count();
        return view('admin.safe.bwzd_list', ['res' => $res,'count'=>$count,'id'=>$id]);
    }

    public function ckbwzd($id)
    {
        $re = DB::table('zhidu')->where('id',$id)->first();
        return view('admin.safe.ckbwzd', ['re' => $re]);
    }


    //安全检查查询
    public function aqjccx(request $request)
    {
        if($request->rows) {
            $map = array();
            $query = DB::table('jiancha');
            if(Auth::guard('admin')->user()->dengji == 2){
                $map['city'] = Auth::guard('admin')->user()->city;
            }
            if(Auth::guard('admin')->user()->dengji == 3){
                $map['area'] = Auth::guard('admin')->user()->area;
            }
            if(Auth::guard('admin')->user()->dengji == 4){
                $unit_id = db::table('units')->where('minjing',Auth::guard('admin')->user()->id)->pluck('id');
                $map['org_id'] = Auth::guard('admin')->user()->org_id;
                $query = DB::table('jiancha')->whereIn('unit_id',$unit_id);
            }

            if($request->city != null){
                if($request->city == 0){

                }else{
                    $map['city'] = $request->city;
                }
            }
            if($request->area){
                $map['area'] = $request->area;
            }
            if($request->org_id){
                $map['org_id'] = $request->org_id;
            }
            if($request->unit_name){
                $unit_id = db::table('units')->where('name',$request->unit_name)->value('id');
                $map[] = array('unit_id',$unit_id);
            }
            if($request->created_at){
                $map[] = array('created_at','like','%'.$request->created_at.'%');
            }
            if($request->name){
                $user_id = db::table('admin_users')->where('name',$request->name)->value('id');
                $map['user_id'] = $user_id;
            }
            if($request->status != null){
                $map['status'] = $request->status;
            }
            if($request->aqjc_type != null){
                $map['aqjc_type'] = $request->aqjc_type;
            }
            $map['is_del'] = '0';
            if($request->city == null && $request->area == null && $request->org_id == null && $request->unit_name == null && $request->created_at == null && $request->name == null && $request->status == null && $request->aqjc_type == null){
                die;
            }
            $res = $query->where($map)->paginate($request->rows);
            $list = $res->items();

            foreach ($list as $k=>$v){
                $list[$k]->city_name = getValue($v->city,'area' ,'id' ,'name' );
                $list[$k]->area_name = getValue($v->area,'area' ,'id' ,'name' );
                $list[$k]->org_name = getValue($v->org_id,'area' ,'id' ,'name' );
                $list[$k]->unit_name = getValue($v->unit_id,'units' ,'id' ,'name' );
                $list[$k]->user = getValue($v->user_id,'admin_users' ,'id' , 'name');
                if($v->status == 1){
                    $list[$k]->status = '合格';
                }elseif($v->status == 2){
                    $list[$k]->status = '不合格';
                }
                if($v->aqjc_type == 1){
                    $list[$k]->aqjc_type = '日常检查';
                }elseif($v->aqjc_type == 2){
                    $list[$k]->aqjc_type = '达标检查';
                }elseif($v->aqjc_type == 3){
                    $list[$k]->aqjc_type = '专项检查';
                }
            }
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.safe.aqjccx');
        }
    }

    public function aqjc_list($id)
    {
        $res = DB::table('jiancha')->where('unit_id',$id)->paginate(10);
        $count = DB::table('jiancha')->where('unit_id',$id)->count();
        return view('admin.safe.aqjc_list', ['res' => $res,'count'=>$count,'id'=>$id]);
    }

    public function ckaqjc($id)
    {
        $re = DB::table('jiancha')->where('id',$id)->first();
        $type = db::table('jiancha')->where('id',$id)->value('type');
        //检查结果
        $res = DB::table('unit_type_aqjc_value')->where('jiancha_id',$re->id)->get();
        return view('admin.safe.ckaqjc', ['res'=>$res,'re' => $re,'type' => $type,'id'=>$id]);
    }


    //安全隐患查询
    public function aqyhcx(request $request)
    {
        if(Auth::guard('admin')->user()->org_id != null){
            $map['org_id'] = Auth::guard('admin')->user()->org_id;
        }
        if($request->isMethod('post')){
            if(input::get('name')){
                $map[] = array('name','like','%'.input::get('name').'%');
            }
            if(input::get('org_id')){
                $map['area_id'] = input::get('org_id');
            }
        }

        $map['is_del'] = '0';
        $res = DB::table('units')->where($map)->orderby('id','desc')->paginate(10);
        $count = DB::table('units')->where($map)->count();

        return view('admin.safe.aqyhcx', ['res' => $res,'count'=>$count,'types'=>'zybwcx']);
    }

    public function aqyh_list($id)
    {
        $res = DB::table('yinhuan')->where('unit_id',$id)->paginate(10);
        $count = DB::table('yinhuan')->where('unit_id',$id)->count();
        return view('admin.safe.aqyh_list', ['res' => $res,'count'=>$count,'id'=>$id]);
    }

    public function ckaqyh($id)
    {
        $re = DB::table('yinhuan')->where('id',$id)->first();
        return view('admin.safe.ckaqyh', ['re' => $re]);
    }


    //不安定因素查询
    public function badyscx(request $request)
    {
        if(Auth::guard('admin')->user()->org_id != null){
            $map['org_id'] = Auth::guard('admin')->user()->org_id;
        }
        if($request->isMethod('post')){
            if(input::get('name')){
                $map[] = array('name','like','%'.input::get('name').'%');
            }
            if(input::get('org_id')){
                $map['area_id'] = input::get('org_id');
            }
        }

        $map['is_del'] = '0';
        $res = DB::table('units')->where($map)->orderby('id','desc')->paginate(10);
        $count = DB::table('units')->where($map)->count();

        return view('admin.safe.badyscx', ['res' => $res,'count'=>$count,'types'=>'zybwcx']);
    }

    public function badys_list($id)
    {
        $res = DB::table('buanding')->where('unit_id',$id)->paginate(10);
        $count = DB::table('buanding')->where('unit_id',$id)->count();
        return view('admin.safe.badys_list', ['res' => $res,'count'=>$count,'id'=>$id]);
    }

    public function ckbadys($id)
    {
        $re = DB::table('buanding')->where('id',$id)->first();
        return view('admin.safe.ckbadys', ['re' => $re]);
    }


    //灾害事故查询
    public function zhsgcx(request $request)
    {
        if(Auth::guard('admin')->user()->org_id != null){
            $map['org_id'] = Auth::guard('admin')->user()->org_id;
        }
        if($request->isMethod('post')){
            if(input::get('name')){
                $map[] = array('name','like','%'.input::get('name').'%');
            }
            if(input::get('org_id')){
                $map['area_id'] = input::get('org_id');
            }
        }

        $map['is_del'] = '0';
        $res = DB::table('units')->where($map)->orderby('id','desc')->paginate(10);
        $count = DB::table('units')->where($map)->count();

        return view('admin.safe.zhsgcx', ['res' => $res,'count'=>$count,'types'=>'zybwcx']);
    }

    public function zhsg_list($id)
    {
        $res = DB::table('zaihai')->where('unit_id',$id)->paginate(10);
        $count = DB::table('zaihai')->where('unit_id',$id)->count();
        return view('admin.safe.zhsg_list', ['res' => $res,'count'=>$count,'id'=>$id]);
    }

    public function ckzhsg($id)
    {
        $re = DB::table('zaihai')->where('id',$id)->first();
        return view('admin.safe.ckzhsg', ['re' => $re]);
    }


    //奖惩信息查询
    public function jcxxcx(request $request)
    {
        if(Auth::guard('admin')->user()->org_id != null){
            $map['org_id'] = Auth::guard('admin')->user()->org_id;
        }
        if($request->isMethod('post')){
            if(input::get('name')){
                $map[] = array('name','like','%'.input::get('name').'%');
            }
            if(input::get('org_id')){
                $map['area_id'] = input::get('org_id');
            }
        }

        $map['is_del'] = '0';
        $res = DB::table('units')->where($map)->orderby('id','desc')->paginate(10);
        $count = DB::table('units')->where($map)->count();

        return view('admin.safe.jcxxcx', ['res' => $res,'count'=>$count,'types'=>'zybwcx']);
    }

    public function jcxx_list($id)
    {
        $res = DB::table('jiangcheng')->where('unit_id',$id)->paginate(10);
        $count = DB::table('jiangcheng')->where('unit_id',$id)->count();
        return view('admin.safe.jcxx_list', ['res' => $res,'count'=>$count,'id'=>$id]);
    }

    public function ckjcxx($id)
    {
        $re = DB::table('jiangcheng')->where('id',$id)->first();
        return view('admin.safe.ckjcxx', ['re' => $re]);
    }













     //警情上报查询
    public function jqsb(request $request){

        if($request->rows) {
            $map['jqsb.is_type'] = '0';
            $query = DB::table('jqsb');
            if(Auth::guard('admin')->user()->dengji == 2){
                $map['city'] = Auth::guard('admin')->user()->city;
            }
            if(Auth::guard('admin')->user()->dengji == 3){
                $map['area'] = Auth::guard('admin')->user()->area;
            }
            if(Auth::guard('admin')->user()->dengji == 4){
                $unit_id = db::table('units')->where('minjing',Auth::guard('admin')->user()->id)->pluck('id');
                $map['org_id'] = Auth::guard('admin')->user()->org_id;
                $query = DB::table('jqsb')->whereIn('unit_id',$unit_id);
            }

            if($request->city != null){
                if($request->city == 0){

                }else{
                    $map['city'] = $request->city;
                }
            }
            if($request->area){
                $map['area'] = $request->area;
            }
            if($request->org_id){
                $map['org_id'] = $request->org_id;
            }
            if($request->unit_name){
                $unit_id = db::table('units')->where('name',$request->unit_name)->value('id');
                $map[] = array('unit_id',$unit_id);
            }
            if($request->created_at){
                $map[] = array('created_at','like','%'.$request->created_at.'%');
            }
            if($request->sbren){
                $map[] = array('sbren','like','%'.$request->sbren.'%');
            }
            if($request->input('police_cl') != null){
                $map['police_cl'] = $request->police_cl;
            }
            if($request->input('chuli_time') != null){
                if($request->input('chuli_time') == 1){
                    $map[] = array('chuli_time','<=',3600*24);
                }
                if($request->input('chuli_time') == 2){
                    $map[] = array('chuli_time','>',3600*24);
                    $map[] = array('chuli_time','<=',3600*24*3);
                }
                if($request->input('chuli_time') == 3){
                    $map[] = array('chuli_time','>',3600*24*3);
                    $map[] = array('chuli_time','<=',3600*24*7);
                }
                if($request->input('chuli_time') == 4){
                    $map[] = array('chuli_time','>',3600*24*7);
                    $map[] = array('chuli_time','<=',3600*24*15);
                }
                if($request->input('chuli_time') == 5){
                    $map[] = array('chuli_time','>',3600*24*15);
                }
            }
            if($request->city == null && $request->area == null && $request->org_id == null && $request->unit_name == null && $request->created_at == null && $request->sbren == null && $request->police_cl == null && $request->chuli_time == null){
                die;
            }
            $res = $query->where($map)->paginate($request->rows);
            $list = $res->items();
            foreach ($list as $k=>$v){
                if($v->police_cl == 1){
                    $list[$k]->chuli_time = format_time($v->chuli_time);
                }
                $list[$k]->city_name = getValue($v->city,'area' ,'id' ,'name' );
                $list[$k]->area_name = getValue($v->area,'area' ,'id' ,'name' );
                $list[$k]->org_name = getValue($v->org_id,'area' ,'id' ,'name' );
                $list[$k]->unit_name = getValue($v->unit_id,'units' ,'id' ,'name' );
                if($v->police_cl == 0){
                    $list[$k]->police_cl = '未处理';
                }else{
                    $list[$k]->police_cl = '已处理';
                }
            }
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{



          return view('admin.safe.jqsb');
        }
    }


    //人员审核查询
    public function rysh(request $request){
        if($request->rows) {
            $map['rysh.is_type'] = '0';
            $query = DB::table('rysh');
            if(Auth::guard('admin')->user()->dengji == 2){
                $map['city'] = Auth::guard('admin')->user()->city;
            }
            if(Auth::guard('admin')->user()->dengji == 3){
                $map['area'] = Auth::guard('admin')->user()->area;
            }
            if(Auth::guard('admin')->user()->dengji == 4){
                $unit_id = db::table('units')->where('minjing',Auth::guard('admin')->user()->id)->pluck('id');
                $map['org_id'] = Auth::guard('admin')->user()->org_id;
                $query = DB::table('rysh')->whereIn('unit_id',$unit_id);
            }

            //如果是民警登录，只能查看自己辖区的单位

            if($request->city != null){
                if($request->city == 0){

                }else{
                    $map['city'] = $request->city;
                }
            }
            if($request->area){
                $map['area'] = $request->area;
            }
            if($request->org_id){
                $map['org_id'] = $request->org_id;
            }
            if($request->sfzh){
                $map[] = array('sfzh','like','%'.$request->sfzh.'%');
            }
            if($request->hcry){
                $map[] = array('hcry','like','%'.$request->hcry.'%');
            }
            if($request->unit_name){
                $unit_id = db::table('units')->where('name',$request->unit_name)->value('id');
                $map[] = array('unit_id',$unit_id);
            }
            if($request->created_at){
                $map[] = array('created_at','like','%'.$request->created_at.'%');
            }
            if($request->police_cl != null){
                $map['police_cl'] = $request->police_cl;
            }
            if($request->input('chuli_time') != null){
                if($request->input('chuli_time') == 1){
                    $map[] = array('chuli_time','<=',3600*24);
                }
                if($request->input('chuli_time') == 2){
                    $map[] = array('chuli_time','>',3600*24);
                    $map[] = array('chuli_time','<=',3600*24*3);
                }
                if($request->input('chuli_time') == 3){
                    $map[] = array('chuli_time','>',3600*24*3);
                    $map[] = array('chuli_time','<=',3600*24*7);
                }
                if($request->input('chuli_time') == 4){
                    $map[] = array('chuli_time','>',3600*24*7);
                    $map[] = array('chuli_time','<=',3600*24*15);
                }
                if($request->input('chuli_time') == 5){
                    $map[] = array('chuli_time','>',3600*24*15);
                }
            }
            if($request->city == null && $request->area == null && $request->org_id == null && $request->unit_name == null && $request->created_at == null && $request->sfzh == null && $request->hcry == null && $request->police_cl == null && $request->chuli_time == null){
                die;
            }
            $res = $query->where($map)->paginate($request->rows);
            $list = $res->items();
            foreach ($list as $k=>$v){
                if($v->police_cl == 1){
                    $list[$k]->chuli_time = format_time($v->chuli_time);
                }
                $list[$k]->city_name = getValue($v->city,'area' ,'id' ,'name' );
                $list[$k]->area_name = getValue($v->area,'area' ,'id' ,'name' );
                $list[$k]->org_name = getValue($v->org_id,'area' ,'id' ,'name' );
                $list[$k]->unit_name = getValue($v->unit_id,'units' ,'id' ,'name' );
                if($v->police_cl == 0){
                    $list[$k]->police_cl = '未处理';
                }else{
                    $list[$k]->police_cl = '已处理';
                }
            }
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
          return view('admin.safe.rysh');
        }
    }

    //重要部位查询
    public function zdgwcx(request $request)
    {
        if($request->rows) {
            $map = array();
            $query = DB::table('zhongdianrenyuan');
            if(Auth::guard('admin')->user()->dengji == 2){
                $map['city'] = Auth::guard('admin')->user()->city;
            }
            if(Auth::guard('admin')->user()->dengji == 3){
                $map['area'] = Auth::guard('admin')->user()->area;
            }
            if(Auth::guard('admin')->user()->dengji == 4){
                $unit_id = db::table('units')->where('minjing',Auth::guard('admin')->user()->id)->pluck('id');
                $map['org_id'] = Auth::guard('admin')->user()->org_id;
                $query = DB::table('zhongdianrenyuan')->whereIn('unit_id',$unit_id);
            }

            if($request->city != null){
                if($request->city == 0){

                }else{
                    $map['city'] = $request->city;
                }
            }
            if($request->area){
                $map['area'] = $request->area;
            }
            if($request->org_id){
                $map['org_id'] = $request->org_id;
            }
            if($request->unit_name){
                $unit_id = db::table('units')->where('name',$request->unit_name)->value('id');
                $map[] = array('unit_id',$unit_id);
            }
            if($request->name){
                $map[] = array('name','like','%'.$request->name.'%');
            }
            if($request->id_number){
                $map[] = array('id_number','like','%'.$request->id_number.'%');
            }
            if($request->city == null && $request->area == null && $request->org_id == null && $request->unit_name == null && $request->name == null && $request->id_number == null){
                die;
            }
            $res = $query->where($map)->paginate($request->rows);
            $list = $res->items();
            foreach ($list as $k=>$v){
                $list[$k]->city_name = getValue($v->city,'area' ,'id' ,'name' );
                $list[$k]->area_name = getValue($v->area,'area' ,'id' ,'name' );
                $list[$k]->org_name = getValue($v->org_id,'area' ,'id' ,'name' );
                $list[$k]->unit_name = getValue($v->unit_id,'units' ,'id' ,'name' );
            }
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.safe.zdgwcx');
        }
    }

    public function ckzdgw($id)
    {
        $re = DB::table('zhongdianrenyuan')->where('id',$id)->first();
        $nation = db::table('nation')->get();
        return view('admin.safe.ckzdgw', ['re' => $re,'id'=>$id,'nation'=>$nation]);
    }

    public function leida(request $request)
    {
         if($request->rows) {
                //如果是民警登录，只能查看自己辖区的单位
                if(Auth::guard('admin')->user()->org_id != null && Auth::guard('admin')->user()->dengji == 4){
                    $map['units.org_id'] = Auth::guard('admin')->user()->org_id;
                }
                //如果是企业用户
                if(Auth::guard('admin')->user()->role_id == 22){
                    $map['units.id'] = Auth::guard('admin')->user()->unit_id;
                }
                if($request->city != null){
                    if($request->city == 0){

                    }else{
                        $map['units.city'] = $request->city;
                    }
                }
                if($request->area){
                    $map['units.area'] = $request->area;
                }
                if($request->org_id){
                    $map['units.org_id'] = $request->org_id;
                }
                if($request->name){
                    $map[] = array('units.name','like','%'.$request->name.'%');
                }
                if($request->frdb_name){
                    $map['units.frdb_name'] = $request->frdb_name;
                }
                if($request->type){
                    $map['units.type'] = $request->type;
                }
                if($request->input('is_renling') != null){
                    $map['is_renling'] = $request->is_renling;
                }
                if($request->input('jibie') != null){
                    $map['is_imp'] = $request->jibie;
                }
                $map['units.is_del'] = '0';
                $res = DB::table('units')
                    ->where($map)
                    ->leftJoin('area as a', 'units.city', '=', 'a.id')
                    ->leftJoin('area as b', 'units.area', '=', 'b.id')
                    ->leftJoin('area as c', 'units.org_id', '=', 'c.id')
                    ->leftJoin('unit_type as d', 'units.type', '=', 'd.id')
                    ->leftJoin('admin_users as e', 'units.user_id', '=', 'e.id')
                    ->select('units.*', 'a.name as city', 'b.name as area', 'c.name as org_id', 'd.name as type', 'e.name as user_id')
                    ->paginate($request->rows);
                $list = $res->items();
                foreach ($list as $k=>$v){
                    if($v->is_renling == 1){
                        $list[$k]->is_renling = '是';
                    }else{
                        $list[$k]->is_renling = '否';
                    }
                    if($v->is_imp == 1){
                        $list[$k]->is_imp = '省级';
                    }elseif($v->is_imp == 2){
                        $list[$k]->is_imp = '市级';
                    }elseif($v->is_imp == 3){
                        $list[$k]->is_imp = '县级';
                    }elseif($v->is_imp == 4){
                        $list[$k]->is_imp = '乡级';
                    }else{
                        $list[$k]->is_imp = '非重点';
                    }
                }
                $result['total'] = $res->total();
                $result['rows'] = $list;
                echo json_encode($result);
            }else{
                //类型
                $type = db::table('unit_type')->where('is_del','0')->get();
                $city = db::table('area')->where('dengji','=','1')->orderby('id')->get();//市
                return view('admin.safe.leida', ['type'=>$type,'city' => $city]);
            }
    }

    //安全检查查询
    public function xsjbcx(request $request)
    {
        if($request->rows) {
            $map = array();
            $query = DB::table('xiansuo');
            if(Auth::guard('admin')->user()->dengji == 2){
                $map['city'] = Auth::guard('admin')->user()->city;
            }
            if(Auth::guard('admin')->user()->dengji == 3){
                $map['area'] = Auth::guard('admin')->user()->area;
            }
            if(Auth::guard('admin')->user()->dengji == 4){
                $unit_id = db::table('units')->where('minjing',Auth::guard('admin')->user()->id)->pluck('id');
                $map['org_id'] = Auth::guard('admin')->user()->org_id;
                $query = DB::table('xiansuo')->whereIn('unit_id',$unit_id);
            }

            if($request->city != null){
                if($request->city == 0){

                }else{
                    $map['city'] = $request->city;
                }
            }
            if($request->area){
                $map['area'] = $request->area;
            }
            if($request->org_id){
                $map['org_id'] = $request->org_id;
            }
            if($request->unit_name){
                $unit_id = db::table('units')->where('name',$request->unit_name)->value('id');
                $map[] = array('unit_id',$unit_id);
            }
            if($request->created_at){
                $map[] = array('created_at','like','%'.$request->created_at.'%');
            }
            if($request->input('police_cl') != null){
                $map['police_cl'] = $request->police_cl;
            }
            if($request->jbr){
                $map[] = array('jbr','like','%'.$request->jbr.'%');
            }
            if($request->input('chuli_time') != null){
                if($request->input('chuli_time') == 1){
                    $map[] = array('chuli_time','<=',3600*24);
                }
                if($request->input('chuli_time') == 2){
                    $map[] = array('chuli_time','>',3600*24);
                    $map[] = array('chuli_time','<=',3600*24*3);
                }
                if($request->input('chuli_time') == 3){
                    $map[] = array('chuli_time','>',3600*24*3);
                    $map[] = array('chuli_time','<=',3600*24*7);
                }
                if($request->input('chuli_time') == 4){
                    $map[] = array('chuli_time','>',3600*24*7);
                    $map[] = array('chuli_time','<=',3600*24*15);
                }
                if($request->input('chuli_time') == 5){
                    $map[] = array('chuli_time','>',3600*24*15);
                }
            }
            if($request->city == null && $request->area == null && $request->org_id == null && $request->unit_name == null && $request->created_at == null && $request->jbr == null && $request->police_cl == null && $request->chuli_time == null){
                die;
            }
            $res = $query->where($map)->paginate($request->rows);
            $list = $res->items();

            foreach ($list as $k=>$v){
                if($v->police_cl == 1){
                    $list[$k]->chuli_time = format_time($v->chuli_time);
                }
                $list[$k]->city_name = getValue($v->city,'area' ,'id' ,'name' );
                $list[$k]->area_name = getValue($v->area,'area' ,'id' ,'name' );
                $list[$k]->org_name = getValue($v->org_id,'area' ,'id' ,'name' );
                $list[$k]->unit_name = getValue($v->unit_id,'units' ,'id' ,'name' );
                if($v->jbdx == 1){
                    $list[$k]->jbdx = '单位';
                }elseif($v->jbdx == 2){
                    $list[$k]->jbdx = '个人';
                }elseif($v->jbdx == 3){
                    $list[$k]->jbdx = '社会';
                }
                if($v->police_cl == 0){
                    $list[$k]->police_cl = '未处理';
                }else{
                    $list[$k]->police_cl = '已处理';
                }
            }
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.safe.xsjbcx');
        }
    }

    public function ckxsjb($id,Request $request){
        // 获取相关信息
        $re = DB::table('xiansuo')->where('xs_id',$id)->first();
        if (Auth::guard('admin')->user()->role_id == 22) {
            $map['id'] = Auth::guard('admin')->user()->unit_id;
        }else{
            $map['id'] = $re->unit_id;
        }
        $units = DB::table('units')->where($map)->first();
        return view('admin.safe.ckxsjb', ['id'=>$id,'re'=>$re,'units'=>$units]);
    }
    

}


